Source for file date.php

Documentation is available at date.php

  1. <?php
  2. /*
  3. Jalali Date function by Milad Rastian (miladmovie AT yahoo DOT com)
  4.  
  5. //The main function which convert Gregorian to Jalali calendars is:
  6. // Copyright (C) 2000  Roozbeh Pournader and Mohammad Toossi
  7. //you can see complete note of those function in down of the page
  8.  
  9. AND JALAI DATE FUNCTION
  10. this function is simillar than date function in PHP
  11. you can find more about it in http://jdf.farsiprojects.com
  12. Copyright (C)2003 FARSI PROJECTS GROUP
  13.  
  14. //   //\\           //        //\\           //          //////
  15. //   //  \\         //        //  \\         //            //
  16. //   //    \\       //        //    \\       //            //
  17. \\  //   /////\\\\\     //        /////\\\\\     //            //
  18. \\//   //        \\   ///////   //        \\   //////////   /////
  19.  
  20. ///////      //\\         //////   ////////
  21. //     //    //  \\         //     //
  22. //     //    //    \\       //     ///////
  23. //     //    /////\\\\\     //     //
  24. ////////     //        \\   //     /////////
  25.  
  26. */
  27. function jdate($type,$maket="now",$tran="")
  28. {
  29.     //set 1 if you want translate number to farsi or if you don't like set 0
  30.     $transnumber=$tran;
  31.     ///chosse your timezone
  32.     $TZhours=0;
  33.     $TZminute=0;
  34.     $need="";
  35.     $result1="";
  36.     $result="";
  37.     if($maket=="now"){
  38.         $year=date("Y");
  39.         $month=date("m");
  40.         $day=date("d");
  41.         list$jyear$jmonth$jday gregorian_to_jalali($year$month$day);
  42.         $maket=mktime(date("H")+$TZhours,date("i")+$TZminute,date("s"),date("m"),date("d"),date("Y"));
  43.     }else{
  44.         //$maket=0;
  45.         $maket+=$TZhours*3600+$TZminute*60;
  46.         $date=date("Y-m-d",$maket);
  47.         list$year$month$day preg_split '/-/'$date );
  48.  
  49.         list$jyear$jmonth$jday gregorian_to_jalali($year$month$day);
  50.     }
  51.  
  52.     $need$maket;
  53.     $year=date("Y",$need);
  54.     $month=date("m",$need);
  55.     $day=date("d",$need);
  56.     $i=0;
  57.     $subtype="";
  58.     $subtypetemp="";
  59.     list$jyear$jmonth$jday gregorian_to_jalali($year$month$day);
  60.     while($i<strlen($type))
  61.     {
  62.         $subtype=substr($type,$i,1);
  63.         if($subtypetemp=="\\")
  64.         {
  65.             $result.=$subtype;
  66.             $i++;
  67.             continue;
  68.         }
  69.  
  70.         switch ($subtype)
  71.         {
  72.  
  73.             case "A":
  74.                 $result1=date("a",$need);
  75.                 if($result1=="pm"$result.= "&#1576;&#1593;&#1583;&#1575;&#1586;&#1592;&#1607;&#1585;";
  76.                 else $result.="&#1602;&#1576;&#1604;&#8207;&#1575;&#1586;&#1592;&#1607;&#1585;";
  77.                 break;
  78.  
  79.             case "a":
  80.                 $result1=date("a",$need);
  81.                 if($result1=="pm"$result.= "&#1576;&#46;&#1592;";
  82.                 else $result.="&#1602;&#46;&#1592;";
  83.                 break;
  84.             case "d":
  85.                 if($jday<10)$result1="0".$jday;
  86.                 else     $result1=$jday;
  87.                 if($transnumber==1$result.=Convertnumber2farsi($result1);
  88.                 else $result.=$result1;
  89.                 break;
  90.             case "D":
  91.                 $result1=date("D",$need);
  92.                 if($result1=="Thu"$result1="&#1662;";
  93.                 else if($result1=="Sat"$result1="&#1588;";
  94.                 else if($result1=="Sun"$result1="&#1609;";
  95.                 else if($result1=="Mon"$result1="&#1583;";
  96.                 else if($result1=="Tue"$result1="&#1587;";
  97.                 else if($result1=="Wed"$result1="&#1670;";
  98.                 else if($result1=="Thu"$result1="&#1662;";
  99.                 else if($result1=="Fri"$result1="&#1580;";
  100.                 $result.=$result1;
  101.                 break;
  102.                 case"F":
  103.                 $result.=monthname($jmonth);
  104.                 break;
  105.             case "g":
  106.                 $result1=date("g",$need);
  107.                 if($transnumber==1$result.=Convertnumber2farsi($result1);
  108.                 else $result.=$result1;
  109.                 break;
  110.             case "G":
  111.                 $result1=date("G",$need);
  112.                 if($transnumber==1$result.=Convertnumber2farsi($result1);
  113.                 else $result.=$result1;
  114.                 break;
  115.             case "h":
  116.                 $result1=date("h",$need);
  117.                 if($transnumber==1$result.=Convertnumber2farsi($result1);
  118.                 else $result.=$result1;
  119.                 break;
  120.             case "H":
  121.                 $result1=date("H",$need);
  122.                 if($transnumber==1$result.=Convertnumber2farsi($result1);
  123.                 else $result.=$result1;
  124.                 break;
  125.             case "i":
  126.                 $result1=date("i",$need);
  127.                 if($transnumber==1$result.=Convertnumber2farsi($result1);
  128.                 else $result.=$result1;
  129.                 break;
  130.             case "j":
  131.                 $result1=$jday;
  132.                 if($transnumber==1$result.=Convertnumber2farsi($result1);
  133.                 else $result.=$result1;
  134.                 break;
  135.             case "l":
  136.                 $result1=date("l",$need);
  137.                 if($result1=="Saturday"$result1="&#1588;&#1606;&#1576;&#1607;";
  138.                 else if($result1=="Sunday"$result1="&#1610;&#1603;&#1588;&#1606;&#1576;&#1607;";
  139.                 else if($result1=="Monday"$result1="&#1583;&#1608;&#1588;&#1606;&#1576;&#1607;";
  140.                 else if($result1=="Tuesday"$result1="&#1587;&#1607;&#32;&#1588;&#1606;&#1576;&#1607;";
  141.                 else if($result1=="Wednesday"$result1="&#1670;&#1607;&#1575;&#1585;&#1588;&#1606;&#1576;&#1607;";
  142.                 else if($result1=="Thursday"$result1="&#1662;&#1606;&#1580;&#1588;&#1606;&#1576;&#1607;";
  143.                 else if($result1=="Friday"$result1="&#1580;&#1605;&#1593;&#1607;";
  144.                 $result.=$result1;
  145.                 break;
  146.             case "m":
  147.                 if($jmonth<10$result1="0".$jmonth;
  148.                 else    $result1=$jmonth;
  149.                 if($transnumber==1$result.=Convertnumber2farsi($result1);
  150.                 else $result.=$result1;
  151.                 break;
  152.             case "M":
  153.                 $result.=short_monthname($jmonth);
  154.                 break;
  155.             case "n":
  156.                 $result1=$jmonth;
  157.                 if($transnumber==1$result.=Convertnumber2farsi($result1);
  158.                 else $result.=$result1;
  159.                 break;
  160.             case "s":
  161.                 $result1=date("s",$need);
  162.                 if($transnumber==1$result.=Convertnumber2farsi($result1);
  163.                 else $result.=$result1;
  164.                 break;
  165.             case "S":
  166.                 $result.="&#1575;&#1605;";
  167.                 break;
  168.             case "t":
  169.                 $result.=lastday ($month,$day,$year);
  170.                 break;
  171.             case "w":
  172.                 $result1=date("w",$need);
  173.                 if($transnumber==1$result.=Convertnumber2farsi($result1);
  174.                 else $result.=$result1;
  175.                 break;
  176.             case "y":
  177.                 $result1=substr($jyear,2,4);
  178.                 if($transnumber==1$result.=Convertnumber2farsi($result1);
  179.                 else $result.=$result1;
  180.                 break;
  181.             case "Y":
  182.                 $result1=$jyear;
  183.                 if($transnumber==1$result.=Convertnumber2farsi($result1);
  184.                 else $result.=$result1;
  185.                 break;
  186.             case "U" :
  187.                 $result.=mktime();
  188.                 break;
  189.             case "Z" :
  190.                 $result.=days_of_year($jmonth,$jday,$jyear);
  191.                 break;
  192.             case "L" :
  193.                 list$tmp_year$tmp_month$tmp_day jalali_to_gregorian(1384121);
  194.                 echo $tmp_day;
  195.                 /*if(lastday($tmp_month,$tmp_day,$tmp_year)=="31")
  196.                 $result.="1";
  197.                 else
  198.                 $result.="0";
  199.                 */
  200.                 break;
  201.             default:
  202.                 $result.=$subtype;
  203.         }
  204.         $subtypetemp=substr($type,$i,1);
  205.         $i++;
  206.     }
  207.     return $result;
  208. }
  209.  
  210.  
  211.  
  212. function jmaketime($hour="",$minute="",$second="",$jmonth="",$jday="",$jyear="")
  213. {
  214.     if(!$hour && !$minute && !$second && !$jmonth && !$jmonth && !$jday && !$jyear)
  215.     return mktime();
  216.     list$year$month$day jalali_to_gregorian($jyear$jmonth$jday);
  217.     $i=mktime($hour,$minute,$second,$month,$day,$year);
  218.     return $i;
  219. }
  220.  
  221.  
  222. ///Find num of Day Begining Of Month ( 0 for Sat & 6 for Sun)
  223. function mstart($month,$day,$year)
  224. {
  225.     list$jyear$jmonth$jday gregorian_to_jalali($year$month$day);
  226.     list$year$month$day jalali_to_gregorian($jyear$jmonth"1");
  227.     $timestamp=mktime(0,0,0,$month,$day,$year);
  228.     return date("w",$timestamp);
  229. }
  230.  
  231. //Find Number Of Days In This Month
  232. function lastday ($month,$day,$year)
  233. {
  234.     $jday2="";
  235.     $jdate2 ="";
  236.     $lastdayen=date("d",mktime(0,0,0,$month+1,0,$year));
  237.     list$jyear$jmonth$jday gregorian_to_jalali($year$month$day);
  238.     $lastdatep=$jday;
  239.     $jday=$jday2;
  240.     while($jday2!="1")
  241.     {
  242.         if($day<$lastdayen)
  243.         {
  244.             $day++;
  245.             list$jyear$jmonth$jday2 gregorian_to_jalali($year$month$day);
  246.             if($jdate2=="1"break;
  247.             if($jdate2!="1"$lastdatep++;
  248.         }
  249.         else
  250.         {
  251.             $day=0;
  252.             $month++;
  253.             if($month==13)
  254.             {
  255.                 $month="1";
  256.                 $year++;
  257.             }
  258.         }
  259.  
  260.     }
  261.     return $lastdatep-1;
  262. }
  263.  
  264. //Find days in this year untile now
  265. function days_of_year($jmonth,$jday,$jyear)
  266. {
  267.     $year="";
  268.     $month="";
  269.     $year="";
  270.     $result="";
  271.     if($jmonth=="01")
  272.     return $jday;
  273.     for ($i=1;$i<$jmonth || $i==12;$i++)
  274.     {
  275.         list$year$month$day jalali_to_gregorian($jyear$i"1");
  276.         $result+=lastday($month,$day,$year);
  277.     }
  278.     return $result+$jday;
  279. }
  280.  
  281. //translate number of month to name of month
  282. function monthname($month)
  283. {
  284.  
  285.     if($month=="01"return "&#1601;&#1585;&#1608;&#1585;&#1583;&#1610;&#1606;";
  286.  
  287.     if($month=="02"return "&#1575;&#1585;&#1583;&#1610;&#1576;&#1607;&#1588;&#1578;";
  288.  
  289.     if($month=="03"return "&#1582;&#1585;&#1583;&#1575;&#1583;";
  290.  
  291.     if($month=="04"return  "&#1578;&#1610;&#1585;";
  292.  
  293.     if($month=="05"return "&#1605;&#1585;&#1583;&#1575;&#1583;";
  294.  
  295.     if($month=="06"return "&#1588;&#1607;&#1585;&#1610;&#1608;&#1585;";
  296.  
  297.     if($month=="07"return "&#1605;&#1607;&#1585;";
  298.  
  299.     if($month=="08"return "&#1570;&#1576;&#1575;&#1606;";
  300.  
  301.     if($month=="09"return "&#1570;&#1584;&#1585;";
  302.  
  303.     if($month=="10"return "&#1583;&#1610;";
  304.  
  305.     if($month=="11"return "&#1576;&#1607;&#1605;&#1606;";
  306.  
  307.     if($month=="12"return "&#1575;&#1587;&#1601;&#1606;&#1583;";
  308. }
  309.  
  310. function short_monthname($month)
  311. {
  312.  
  313.     if($month=="01"return "&#1601;&#1585;&#1608;";
  314.  
  315.     if($month=="02"return "&#1575;&#1585;&#1583;";
  316.  
  317.     if($month=="03"return "&#1582;&#1585;&#1583;";
  318.  
  319.     if($month=="04"return  "&#1578;&#1610;&#1585;";
  320.  
  321.     if($month=="05"return "&#1605;&#1585;&#1583;";
  322.  
  323.     if($month=="06"return "&#1588;&#1607;&#1585;";
  324.  
  325.     if($month=="07"return "&#1605;&#1607;&#1585;";
  326.  
  327.     if($month=="08"return "&#1570;&#1576;&#1575;";
  328.  
  329.     if($month=="09"return "&#1570;&#1584;&#1585;";
  330.  
  331.     if($month=="10"return "&#1583;&#1610;";
  332.  
  333.     if($month=="11"return "&#1576;&#1607;&#1605;";
  334.  
  335.     if($month=="12"return "&#1575;&#1587;&#1601; ";
  336. }
  337.  
  338. ////here convert to  number in persian
  339. function Convertnumber2farsi($srting)
  340. {
  341.     $num0="&#1776;";
  342.     $num1="&#1777;";
  343.     $num2="&#1778;";
  344.     $num3="&#1779;";
  345.     $num4="&#1780;";
  346.     $num5="&#1781;";
  347.     $num6="&#1782;";
  348.     $num7="&#1783;";
  349.     $num8="&#1784;";
  350.     $num9="&#1785;";
  351.  
  352.     $stringtemp="";
  353.     $len=strlen($srting);
  354.     for($sub=0;$sub<$len;$sub++)
  355.     {
  356.         if(substr($srting,$sub,1)=="0")$stringtemp.=$num0;
  357.         elseif(substr($srting,$sub,1)=="1")$stringtemp.=$num1;
  358.         elseif(substr($srting,$sub,1)=="2")$stringtemp.=$num2;
  359.         elseif(substr($srting,$sub,1)=="3")$stringtemp.=$num3;
  360.         elseif(substr($srting,$sub,1)=="4")$stringtemp.=$num4;
  361.         elseif(substr($srting,$sub,1)=="5")$stringtemp.=$num5;
  362.         elseif(substr($srting,$sub,1)=="6")$stringtemp.=$num6;
  363.         elseif(substr($srting,$sub,1)=="7")$stringtemp.=$num7;
  364.         elseif(substr($srting,$sub,1)=="8")$stringtemp.=$num8;
  365.         elseif(substr($srting,$sub,1)=="9")$stringtemp.=$num9;
  366.         else $stringtemp.=substr($srting,$sub,1);
  367.  
  368.     }
  369.     return   $stringtemp;
  370.  
  371. }///end conver to number in persian
  372.  
  373. function is_kabise($year)
  374. {
  375.     if($year%4==&& $year%100!=0)
  376.     return true;
  377.     return false;
  378. }
  379.  
  380.  
  381. function jcheckdate($month,$day,$year)
  382. {
  383.     $j_days_in_month array(313131313131303030303029);
  384.     if($month<=12 && $month>0)
  385.     {
  386.         if($j_days_in_month[$month-1]>=$day &&     $day>0)
  387.         return 1;
  388.         //if(is_kabise($year))
  389.         //echo "Asdsd";
  390.         if(is_kabise($year&& $j_days_in_month[$month-1]==31)
  391.         return 1;
  392.     }
  393.  
  394.     return 0;
  395.  
  396. }
  397.  
  398. function jtime()
  399. {
  400.     return mktime()    ;
  401. }
  402.  
  403. function jgetdate($timestamp="")
  404. {
  405.     if($timestamp=="")
  406.     $timestamp=mktime();
  407.  
  408.     return array(
  409.     0=>$timestamp,
  410.     "seconds"=>jdate("s",$timestamp),
  411.     "minutes"=>jdate("i",$timestamp),
  412.     "hours"=>jdate("G",$timestamp),
  413.     "mday"=>jdate("j",$timestamp),
  414.     "wday"=>jdate("w",$timestamp),
  415.     "mon"=>jdate("n",$timestamp),
  416.     "year"=>jdate("Y",$timestamp),
  417.     "yday"=>days_of_year(jdate("m",$timestamp),jdate("d",$timestamp),jdate("Y",$timestamp)),
  418.     "weekday"=>jdate("l",$timestamp),
  419.     "month"=>jdate("F",$timestamp),
  420.     );
  421. }
  422.  
  423.  
  424.  
  425. // "jalali.php" is convertor to and from Gregorian and Jalali calendars.
  426. // Copyright (C) 2000  Roozbeh Pournader and Mohammad Toossi
  427. //
  428. // This program is free software; you can redistribute it and/or
  429. // modify it under the terms of the GNU General Public License
  430. // as published by the Free Software Foundation; either version 2
  431. // of the License, or (at your option) any later version.
  432. //
  433. // This program is distributed in the hope that it will be useful,
  434. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  435. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  436. // GNU General Public License for more details.
  437. //
  438. // A copy of the GNU General Public License is available from:
  439. //
  440. //    <a href="http://www.gnu.org/copyleft/gpl.html" target="_blank">http://www.gnu.org/copyleft/gpl.html</a>
  441. //
  442.  
  443.  
  444. function div($a,$b{
  445.     return (int) ($a $b);
  446. }
  447.  
  448. function gregorian_to_jalali ($g_y$g_m$g_d)
  449. {
  450.     $g_days_in_month array(312831303130313130313031);
  451.     $j_days_in_month array(313131313131303030303029);
  452.  
  453.  
  454.  
  455.  
  456.  
  457.     $gy $g_y-1600;
  458.     $gm $g_m-1;
  459.     $gd $g_d-1;
  460.  
  461.     $g_day_no 365*$gy+div($gy+3,4)-div($gy+99,100)+div($gy+399,400);
  462.  
  463.     for ($i=0$i $gm++$i)
  464.     $g_day_no += $g_days_in_month[$i];
  465.     if ($gm>&& (($gy%4==&& $gy%100!=0|| ($gy%400==0)))
  466.     /* leap and after Feb */
  467.     $g_day_no++;
  468.     $g_day_no += $gd;
  469.  
  470.     $j_day_no $g_day_no-79;
  471.  
  472.     $j_np div($j_day_no12053)/* 12053 = 365*33 + 32/4 */
  473.     $j_day_no $j_day_no 12053;
  474.  
  475.     $jy 979+33*$j_np+4*div($j_day_no,1461)/* 1461 = 365*4 + 4/4 */
  476.  
  477.     $j_day_no %= 1461;
  478.  
  479.     if ($j_day_no >= 366{
  480.         $jy += div($j_day_no-1365);
  481.         $j_day_no ($j_day_no-1)%365;
  482.     }
  483.  
  484.     for ($i 0$i 11 && $j_day_no >= $j_days_in_month[$i]++$i)
  485.     $j_day_no -= $j_days_in_month[$i];
  486.     $jm $i+1;
  487.     $jd $j_day_no+1;
  488.  
  489.     return array($jy$jm$jd);
  490. }
  491.  
  492. function jalali_to_gregorian($j_y$j_m$j_d)
  493. {
  494.     $g_days_in_month array(312831303130313130313031);
  495.     $j_days_in_month array(313131313131303030303029);
  496.  
  497.  
  498.  
  499.     $jy $j_y-979;
  500.     $jm $j_m-1;
  501.     $jd $j_d-1;
  502.  
  503.     $j_day_no 365*$jy div($jy33)*div($jy%33+34);
  504.     for ($i=0$i $jm++$i)
  505.     $j_day_no += $j_days_in_month[$i];
  506.  
  507.     $j_day_no += $jd;
  508.  
  509.     $g_day_no $j_day_no+79;
  510.  
  511.     $gy 1600 400*div($g_day_no146097)/* 146097 = 365*400 + 400/4 - 400/100 + 400/400 */
  512.     $g_day_no $g_day_no 146097;
  513.  
  514.     $leap true;
  515.     if ($g_day_no >= 36525/* 36525 = 365*100 + 100/4 */
  516.     {
  517.         $g_day_no--;
  518.         $gy += 100*div($g_day_no,  36524)/* 36524 = 365*100 + 100/4 - 100/100 */
  519.         $g_day_no $g_day_no 36524;
  520.  
  521.         if ($g_day_no >= 365)
  522.         $g_day_no++;
  523.         else
  524.         $leap false;
  525.     }
  526.  
  527.     $gy += 4*div($g_day_no1461)/* 1461 = 365*4 + 4/4 */
  528.     $g_day_no %= 1461;
  529.  
  530.     if ($g_day_no >= 366{
  531.         $leap false;
  532.  
  533.         $g_day_no--;
  534.         $gy += div($g_day_no365);
  535.         $g_day_no $g_day_no 365;
  536.     }
  537.  
  538.     for ($i 0$g_day_no >= $g_days_in_month[$i($i == && $leap)$i++)
  539.     $g_day_no -= $g_days_in_month[$i($i == && $leap);
  540.     $gm $i+1;
  541.     $gd $g_day_no+1;
  542.  
  543.     return array($gy$gm$gd);
  544. }
  545. function mlj($date)//for find jmaketime date
  546.     global $mosConfig_offset;
  547.     //split 0000-00-00 00:00:00
  548.     if(ereg("([0-9]{4})-([0-9]{2})-([0-9]{2})[ ]([0-9]{2}):([0-9]{2}):([0-9]{2})",$date,$arg)){
  549.         $year    $arg[1];
  550.         $mon    $arg[2];
  551.         $day    $arg[3];
  552.         $hour    $arg[4];
  553.         $min    $arg[5];
  554.         $sec    $arg[6];
  555.     }elseif (ereg("([0-9]{4})-([0-9]{2})-([0-9]{2})",$date,$arg))//split 0000-00-00
  556.         $year    $arg[1];
  557.         $mon    $arg[2];
  558.         $day    $arg[3];
  559.         $hour    0;
  560.         $min    0;
  561.         $sec    0;
  562.     }else{
  563.         return strtotime($date);
  564.     }
  565.     $mdate jmaketime($hour,$min,$sec,$mon,$day,$year)-($mosConfig_offset 60 60);
  566.     return $mdate;
  567. }
  568. ?>

Documentation generated on Mon, 05 May 2008 16:19:13 +0400 by phpDocumentor 1.4.0